package de.hda.vo;

import java.util.HashSet;
import java.util.Set;

public class HandlerVO implements IHandlerObject, Cloneable {
    
    /** true if new value sent to any setter */
    private boolean dirty = false;

	/**
	 * A set with all the fieldNames of the changed fields
	 */
	private Set<String> changedFields;

    /** creates a copy of the current object */
    public Object clone() {
        try {
            return super.clone();
        } catch( CloneNotSupportedException e) {
            // This should never happen
            throw new InternalError(e.toString());
        }
    }
    
    /** clients can use dirty flag */
    public boolean isDirty() {
        return dirty;
    }

    public void resetDirty() {
    	
        this.dirty = false;
        if (this.changedFields != null)
        	this.changedFields.clear();
    }

    public void markAsDirty() {
        this.dirty = true;
    }

    public void markAsNotDirty() {
        this.dirty = false;
    }

	public void markFieldChanged(String fieldName) {
		
		if (changedFields == null)
			changedFields = new HashSet<String>();
		
		changedFields.add(fieldName);
	}
 
	public boolean hasFieldChanged(String fieldName) {
		
		if (changedFields == null)
			return false;
		
		return changedFields.contains(fieldName);
	}
	
	public Set<String> getChangedFields() {
		
		return this.changedFields;
	}
}

